<?php
// +----------------------------------------------------------------------
// | 搜索控制器
// +----------------------------------------------------------------------

class search_controller extends base_controller {

	public function _init(){
		$this->getLeftData();
		$this->getFooterData();
	}

	public function index(){
		//如果是第一次POST提交搜索则跳转到seo网址上
		if (isset ( $_POST['keyword'] )) {
			$keyword = $_POST['keyword'];
			if(!empty($keyword)){
				header ( 'location:' . url('keyword', $keyword) );
			}else{
				header ( 'location:' . $this->url['www']);
			}
			exit();
		}
		$keyword = urldecode($_GET['keyword']);
		$page = $_GET[C('var_page')] = (int)$_GET['page'];
		if($page<=0) $page = 1;

		//================= 检查静态页缓存 ======================
		$cachefile = $this->getKeywordCacheFile('keyword',$keyword,$page);
		if(is_file($cachefile)){
			//缓存存在
			if((time() - filemtime($cachefile)) < C('keyword_cache_time')) {
				echo file_get_contents($cachefile);
				exit();
			}
		}

		//=========== 搜索处理开始 并生成缓存 ===========
		C('sys_page_listrows',20);
		$content = $this->_index($keyword, $page);
		$search_num = $this->keywords->field("id,search_num")->where("name='$keyword'")->getField('search_num');
		if(is_numeric($search_num) && $search_num>0){
			if(!is_dir(dirname($cachefile))) mk_dir(dirname($cachefile));
			file_put_contents($cachefile, $content);
		}
		echo $content;
	}

	//搜索首页
	public function _index($keyword,$page){
		$var = array();
		
		$var['keyword'] = $keyword;

		//全部分类
		$var['cates'] = $this->category->getChildTree(0);

		//字母列表
		$var['letters'] = $this->category->getLetterList();

		//搜索列表
		$field = 'id,name,catid1,catid2,img,view,download,addtime,updatetime';
		$conf = array('index_url' => url('keyword',$keyword), 'url' => url('keyword',$keyword).'{p}.html', 'setTheme' => 'en', 'theme'=>' %firstPage%  %prePage%  %linkPage%  %nextPage%  %lastPage%');
		$rows = $this->image->field($field)->where("match(name) against('$keyword')")->page($page,$conf);
		$rows['lists'] = $this->image->format($rows['lists']);
		if($rows) $var = array_merge($var,$rows);

		//热门关键词
		$rows = $this->keywords->getHotKeywords(20,$cate['name']);
		$var['hot_keywords'] = $this->keywords->format($rows);

		$seo_page = $page>1 ? " page $page" : '';
		$this->assign('seo_title',$keyword.', Android & iPhone & iPad Wallpapers - Free '.$keyword.' Wallpapers & Pictures Download'.$seo_page);
		$this->assign('seo_keywords',$keyword.' Android & iPhone & iPad Wallpapers, wallpapers free download');
		$this->assign('seo_description','Free '.$keyword.' Android & iPhone & iPad wallpapers download, download free High Resolution, HD, Widescreen  wallpapers and pictures.');

		$this->assign($var);
		$content = $this->fetch('search_index');
		return $content;
	}

	//获取软件页缓存地址
	protected function getKeywordCacheFile($type,$name,$page){
		$name = name2dir($name);
		$md5 = md5($name);
		$dir = substr($md5,0,1).'/'.substr($md5,1,1);
		$file = CACHE_PATH."/$type/$dir/$name-$page.html";
		return $file;
	}

}